package com.datasure.demo.webSocket.config;

import com.datasure.demo.webSocket.services.BasicService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaolong5.zhang
 * @date 2022/05/23 20:20
 **/
@Component
public class ChannelManage {
    private Map<ChannelId, BasicService> channelIdMap = new HashMap<>();

    /**
     *
     * @param channel
     * @param url  /token/业务类型/key1=val1&key2=val2
     */
    public void add(Channel channel, String url){
        String[] split = url.split("\\/");
        String token = split[1];
        String bussineType = split[2];
        String paramStr = split[3];
        BasicService service = ServiceManage.getService(bussineType);
        channelIdMap.put(channel.id(), service);

        service.addUser(channel.id(), RequestInfo.builder()
                .userId(10000L)
                .paramsStr(paramStr)
                .channel(channel)
                .build());

    }

    public void remove(Channel channel){
        BasicService basicService = channelIdMap.get(channel.id());
        basicService.removeUser(channel.id());
        channelIdMap.remove(channel.id());

    }
}
